﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using JtextEpicsInterface;

namespace JEPlugIn_heartBeats
{
    /// <summary>
    /// this plugin make the value go up and down at a preset rate ,heart beat rate is set by scan interval of this value
    /// </summary>
    public class PlugInHeartBeats : IJEPlugIn
    {
        const string MyName = "HeartBeats";
        double High;
        double Low;
        double HeartRate;
        double HeartVal;
        double Direction;

        public string PlugInName 
        {
            get
            {
                return MyName;
            }
        }

        /// <summary>
        /// Initialize the plug in.
        /// <para>This is call onec at the beginning for the server start up,and the record is not going to initialize any thing later so finish all the initialize job.
        /// <para>the heartVal is give to the Record who called this and the heartVal increasce by the heartRate,till it reached the lo,hi limit and then it go reverse</para>
        /// </summary>
        /// <param name="iniObject">hi lo rate,ini_value.</param>
        public void IniPlugIn(object iniObject,IJERecord record)    //usually a string
        {
            string StrConfig = (string)iniObject;//hi lo rate,ini_value
            string[] StrConfigs = Regex.Split(StrConfig, ";", RegexOptions.IgnoreCase);
            int Config_index = StrConfig.IndexOf("HeartBeats")+1;
            High = Convert.ToDouble(StrConfigs[Config_index+0]);
            Low = Convert.ToDouble(StrConfigs[Config_index + 1]);
            HeartRate = Math.Abs(Convert.ToDouble(StrConfigs[Config_index+2]));
            HeartVal = Convert.ToDouble(StrConfigs[Config_index+3]);
            Direction = 1;

        }

        /// <summary>
        /// Runs the plug in.
        /// <Para>the record will call this each time it get flushed,for input and out put there  maybe differences.Look up example if you are making plugin</Para>
        /// </summary>
        /// <param name="myRecord">My record.The record that called the plugIn,you can set get and do anything</param>
        /// <returns>if successful</returns>
        public  bool RunPlugIn(IJERecord myRecord)
        {
            
            Beats();
            myRecord.VAL = HeartVal;
            Console.WriteLine(myRecord.pvName + " called me!!  " + myRecord.VAL.ToString()); 
            return true;

        }

        public void Beats()
        {
            if (HeartVal > High)
            {
                Direction = -1;
            }
            else if (HeartVal <Low )
            {
                Direction = 1;
            }
            HeartVal += HeartRate*Direction;

        }


        /// <summary>
        /// Kills the plug in when you don't need it.
        /// </summary>
        public void KillPlugIn()
        { //nothing to fuck
        }
    }
}
